package com.hotstrip.web;

import com.hotstrip.common.line.LineBotConfig;
import com.hotstrip.model.R;
import com.hotstrip.model.enums.ResponseCodeEnums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2019/7/30.
 */
@Controller
public class IndexController {
    private static Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Resource
    private LineBotConfig lineBotConfig;

    /**
     * 首页
     * @param model
     * @return
     */
    @GetMapping({"/", "index"})
    public String index(Model model){
        model.addAttribute("channelId", lineBotConfig.getChannelId());
        return "index";
    }

    /**
     * 登录
     * @param channelId
     * @param channelSecret
     * @return
     */
    @ResponseBody
    @PostMapping("/login")
    public Object login(String channelId,
                        String channelSecret){
        logger.info("this is login route, channelId: {}, and channelSecret: {}",
                channelId, channelSecret);
        if (lineBotConfig.getChannelId().equals(channelId) && lineBotConfig.getChannelSecret().equals(channelSecret)){
            return R.ok("success");
        }
        logger.error("invalid param......");
        // 添加错误描述
        return R.error(ResponseCodeEnums.INVALID_PARAM.getValue(), "invalid param");
    }
}
